package com.android.hedgehog;

import java.util.List;
import java.util.Random;

import android.graphics.Color;

import com.android.hedgehog.framework.Game;
import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Input.TouchEvent;
import com.android.hedgehog.puzzles.CardMatchScreen;
import com.android.hedgehog.puzzles.GoalieScreen;
import com.android.hedgehog.puzzles.GoalieScreen2;
import com.android.hedgehog.puzzles.MathScreen;
import com.android.hedgehog.puzzles.PuzzleScreen;
import com.android.hedgehog.puzzles.TimeBombScreen;

public class BattleEngine {
	static final Random rand = new Random();
	Graphics graphics;
	public int playerScore, botScore;
	public String[] puzzleTitles = { "Math", "Card", "Goalie", "TimeBomb" };
	public String currentPuzzle = "";
	public String resultMsg;
	public PuzzleScreen puzzle;
	public boolean turnFinished;

	int gameNumber;
	Game game;
	int difficulty;
	double turnTime;
	double totalTime;
	int currentInstanceNum;
	int totalInstances;
	int turnTimeLimit;
	boolean success;

	public BattleEngine(Game game) {
		this.game = game;
		graphics = game.getGraphics();
	}

	public void startPuzzle(int difficulty, double totalTime) {
		this.difficulty = difficulty;
		this.totalTime = totalTime;
		currentInstanceNum = 1;
		turnFinished = false;
		success = false;
		turnTime = 0;

		if (gameNumber == 0) {
			puzzle = new MathScreen(game, 0, difficulty, currentInstanceNum);
			totalInstances = 2;
			turnTimeLimit = 48;
		}

		if (gameNumber == 1) {
			puzzle = new CardMatchScreen(game, 0, difficulty, currentInstanceNum);
			totalInstances = 1;
			turnTimeLimit = 40;
		}
		if (gameNumber == 2) {
			puzzle = new GoalieScreen2(game, 0, difficulty, currentInstanceNum);
			totalInstances = 2;
			turnTimeLimit = 30;
		}
		if (gameNumber == 3) {
			puzzle = new TimeBombScreen(game, 0, difficulty, currentInstanceNum);
			totalInstances = 1;
			turnTimeLimit = 35;
		}
	}

	public void extraPuzzle(int difficulty, double totalTime) {
		this.difficulty = difficulty;
		this.totalTime = totalTime;
		if (gameNumber == 0) {
			puzzle = new MathScreen(game, 0, difficulty, currentInstanceNum);
		}
		if (gameNumber == 1) {
			puzzle = new CardMatchScreen(game, 0, difficulty, currentInstanceNum);
		}
		if (gameNumber == 2) {
			puzzle = new GoalieScreen2(game, 0, difficulty, currentInstanceNum);
		}
		if (gameNumber == 3) {
			puzzle = new TimeBombScreen(game, 0, difficulty, currentInstanceNum);
		}
	}

	public void chooseGameForRound() {
		gameNumber = rand.nextInt(puzzleTitles.length);
		currentPuzzle = puzzleTitles[gameNumber] + " Round";

	}

	public void updatePuzzle(List<TouchEvent> touchEvents, double deltaTime) {
		turnTime += deltaTime;
		puzzle.update(touchEvents, deltaTime);
		if (puzzle.instanceOver) { // puzzle instance FINISHED
			isTurnOver();
		}// puzzle instance finished
		if ((turnTimeLimit - (int) turnTime) < 0) {// TIME OVER
			if (puzzle.instanceScore > 0) {
				playerScore += difficulty;
				success = true;
			}// RARE CASE if player finishes right before time over
			turnFinished = true;

		}// TIME OVER

	}

	private void isTurnOver() {
		currentInstanceNum++;
		if (puzzle.instanceScore > 0) {// if instance success
			if (totalInstances >= currentInstanceNum) {
				extraPuzzle(difficulty, totalTime);
			} else {
				playerScore += difficulty;
				success = true;
				turnFinished = true;
			}
		} else {// if instance fail
			turnFinished = true;
		}

	}

	public void presentPuzzle() {
		graphics.drawText("" + (turnTimeLimit - (int) turnTime), 260, 20, 20, Color.MAGENTA);
		graphics.drawText("Question " + currentInstanceNum + " of " + totalInstances, 130, 20, 18,
				Color.MAGENTA);
		puzzle.present();
	}

}
